﻿using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using SaiLing.Web;

namespace SaiLing.SingleSignOn.Application.Core.Authorizations
{
    //public class RequiredSignInAttribute : IAuthorizeFilter
    //{
    //    public virtual void OnAuthorization(AuthorizationContext filterContext)
    //    {
    //        if (filterContext == null)
    //        {
    //            throw new ArgumentNullException("filterContext");
    //        }
    //        var requestContext = filterContext.RequestContext;
    //        if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
    //        {
    //            if (!AuthorizeCore(filterContext.RequestContext))
    //            {
    //                if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
    //                {
    //                    var jsonResult = new JsonResultData() { Success = false };
    //                    jsonResult.AddErrorMessage("没有访问权限.");
    //                    filterContext.Result = new JsonResult() { Data = jsonResult, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    //                }
    //                else
    //                {
    //                    var url = FormsAuthentication.LoginUrl.AddQueryParam("return", filterContext.HttpContext.Request.Url.ToString());
    //                    filterContext.Result = new RedirectResult(UrlUtility.ToHttpAbsolute(url));
    //                }
    //            }
    //        }
    //    }

    //    protected virtual bool AuthorizeCore(RequestContext requestContext)
    //    {
    //        var authenticated = requestContext.HttpContext.User.Identity.IsAuthenticated;
    //        return authenticated;
    //    }

    //    public bool RequiredAdministrator { get; set; }
    //    public bool Exclusive { get; set; }
    //}


    //public class AuthorizationAttribute : RequiredSignInAttribute
    //{
    //    public override void OnAuthorization(AuthorizationContext filterContext)
    //    {
    //        var allowAnonymous = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
    //        if (!allowAnonymous)
    //        {
    //            if (base.AuthorizeCore(filterContext.RequestContext))
    //            {
    //                if (!filterContext.RequestContext.Authorize())
    //                {
    //                    HandleUnauthorizedRequest(filterContext);
    //                }
    //            }
    //            else
    //            {
    //                var url = FormsAuthentication.LoginUrl.AddQueryParam("return",
    //                    filterContext.HttpContext.Request.Url.ToString());
    //                filterContext.Result = new RedirectResult(UrlUtility.ToHttpAbsolute(url));
    //            }
    //        }

    //    }


    //    protected virtual void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    //    {
    //        if (!filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
    //            filterContext.Result = new RedirectResult("~/NoPermission");
    //        else
    //        {
    //            var jsonResult = new JsonResultData() { Success = false };
    //            jsonResult.AddErrorMessage("没有访问权限.");
    //            filterContext.Result = new JsonResult() { Data = jsonResult, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    //        }
    //    }
    //}
    //public static class AuthorizationHelpers
    //{
    //    public static bool Authorize(this RequestContext requestContext)
    //    {
    //        IPrincipal user = requestContext.HttpContext.User;
    //        if (!user.Identity.IsAuthenticated)
    //        {
    //            return false;
    //        }
    //        return true;
    //        //return ServiceFactory.UserService.Authorize(user.Identity.Name, requestContext.GetRequestValue("controller"), requestContext.GetRequestValue("action"));
    //    }
    //}
}
